package com.catbad.controller;

import com.catbad.domain.*;
import com.catbad.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserTicketController {
    @Autowired
    CinemaServices cinemaServices;
    @Autowired
    RoomServices roomServices;
    @Autowired
    PlayServices playServices;
    @Autowired
    FilmServices filmServices;
    @Autowired
    OrderServices orderServices;

    /**
     * 放映厅信息选择显示
     * @param cid
     * @param filmId
     * @param model
     * @return
     */
    @RequestMapping("room")
    public String room(Long cid, Long filmId, Model model){
        //获得所有放映厅，和对应电影的场次信息以及电影的时长
        List<Map<String,Object>> roomList = roomServices.getRoomListForPage(cid,filmId);
        Film film = filmServices.getFilmById(filmId);
        Cinema cinema = cinemaServices.getCinemaById(cid);
        model.addAttribute("roomList",roomList);
        model.addAttribute("filmId",filmId);
        model.addAttribute("film",film);
        model.addAttribute("cinema",cinema);
        return "user/room";
    }

    /**
     * 场次信息
     * @param cid
     * @param roomId
     * @param filmId
     * @param model
     * @return
     */
    @RequestMapping("play")
    public String play(Long cid,Long roomId, Long filmId, Model model){
        //得到改放映厅吗，该电影的所有场次
        List<Map<String,Object>> playList = playServices.getPlayList(roomId,filmId);
        Map<String,Object> film =filmServices.getFilmMapById(filmId);
        Cinema cinema = cinemaServices.getCinemaById(cid);
        Room room = roomServices.getRoomById(roomId);
        model.addAttribute("playList",playList);
        model.addAttribute("film",film);
        model.addAttribute("filmId",filmId);
        model.addAttribute("cinema",cinema);
        model.addAttribute("room",room);
        return "user/play";
    }

    /**
     * 进入选座页面
     * @param pid
     * @param model
     * @return
     */
    @RequestMapping("goupiao")
    public String goupiao(Long pid,Model model){
        Play play = playServices.getPlayById(pid);
        Long filmId = play.getFilmId();
        Long roomId = play.getRoomId();
        Map<String,Object> film =filmServices.getFilmMapById(filmId);
        Room room = roomServices.getRoomById(roomId);
        model.addAttribute("play",play);
        model.addAttribute("pid",pid);
        model.addAttribute("film",film);
        model.addAttribute("filmId",filmId);
        model.addAttribute("room",room);
        return "user/goupiao";
    }

    /**
     * 影厅座位信息，返回渲染
     * @param pid
     * @return
     */
    @RequestMapping("mai")
    @ResponseBody
    public List<String> mai(Long pid){
        //统计该场次所有订单的，座位信息，为一个集合返回
        return orderServices.getSeatsByPid(pid);
    }

    @RequestMapping("goumai")
    @ResponseBody
    public Integer goumai(Filmorder filmorder, HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        filmorder.setUid(user.getUid());
        int result = orderServices.saveOrder(filmorder);
        if(result>0){
            return filmorder.getCnt();
        }
        return 0;
    }

    /**
     * 三级下拉列表影院下拉
     * @param aid
     * @return
     */
    @RequestMapping("cinema3")
    @ResponseBody
    public Map<String,Object> cinema3(Long aid){
        Map<String,Object> ret = new HashMap<>();
        List<Cinema> cinemaList = cinemaServices.selectCinemaByAid(aid);
        ret.put("code",0);
        ret.put("data",cinemaList);
        return ret;
    }
}
